package com.android.wellcherish.kotlindemo1

import java.lang.IllegalArgumentException
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass

/**
 * @author 张文琛
 * @date 2020/7/14 21:56
 *
 *
 */
class CarFactory {
    /**
     * 伴生对象相当于静态对象
     * */
    var carList: MutableList<AbCar> = mutableListOf()

    init {
        addCar(Bicycle())
        addCar(Motorcycle())
        addCar(Bus())
    }

    /**
     * 增加车的种类
     * */
    fun addCar(car: AbCar) {
        if(carList.contains(car)) {
            return
        }
        carList.add(car)
    }
    /**
     * 去除车的种类
     * */
    fun removeCar(car: AbCar) {
        carList.remove(car)
    }
    fun removeCar(name: String) {
        var car: AbCar? = null
        carList.forEach {
            if(it.getName() == name) {
                car = it
            }
        }

        if(car == null) {
            carList.remove(car)
        }
    }
    /**
     * 去除所有车的种类
     * */
    fun clearAllCars() {
        println("销毁所有汽车：${carList}")
        carList.clear()
    }

    /**
     * 创建特定车辆
     * */
    fun produceOneCar(name: String): AbCar? {
        var car: AbCar? = null
        carList.forEach {
            if(it.getName() == name) {
                var clazz: Class<AbCar> = it.javaClass
                return clazz.getConstructor().newInstance()
            }
        }
        return car
    }
}